package com.wxq.schoolmap;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.lxr.activity.GetPicActivity;
import com.wxq.draw.DrawDBTool;
import com.wxq.gps.SpotGPSActivity;
import com.wxq.utility.QUtility;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SpotDetailActivity extends Activity {

	EditText spotIdET;
	EditText DisplayET;
	EditText CompanyNameET;
	EditText MapNameET;
	EditText TypeET;
	EditText AddressET;
	EditText PhoneET;
	EditText EmailET;
	EditText OpeningTimeET;
	EditText Coord_XET;
	EditText Coord_YET;
	EditText image;//lxr imageid!!!!!!!!!!
	LinearLayout moreInfoLayout;
	Button moreInfoBtn;
	
	
	Button getPicBtn;
	Button modifyBtn;
	Button cancelBtn;
	Button commitBtn;
	Button deleteBtn;
	View modifyBar;
	Button gpsBtn;
	Button unitTypeBtn;
	
	String spotId;
	String unitTypeId;
	String xmlContent;
	String removed = "";
	String MallDBpath;
	DrawDBTool dbTool;
	int unitIndex = 0;
	
	Map<String, String> unitTypeMap ;
	List<Map<String, String>> unitTypeList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spot_detail);
		
		Intent intent = getIntent();
		spotId = intent.getStringExtra("spotId");
		MallDBpath = intent.getStringExtra("MallDBpath"); 
		Log.d("lxr", spotId+"   "+MallDBpath);
		
		dbTool = new DrawDBTool(this);
		dbTool.setDBpath(MallDBpath);
		
		unitTypeMap = dbTool.getUnitTypeMap();
		unitTypeList = DrawDBTool.getUnitTypeList(unitTypeMap);
		
		getAllViews();
		
		modifyBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				modifyBar.setVisibility(View.VISIBLE);
				enableAllViews();
			}
		});
		getPicBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 打开拍照页面进行拍照
				Intent intent = new Intent();
				intent.putExtra("spotId", spotId);
				intent.putExtra("MallDBpath", MallDBpath);
				intent.setClass(SpotDetailActivity.this, GetPicActivity.class);
				SpotDetailActivity.this.startActivity(intent);
			}
		});
		cancelBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 取消修改
				//fillAllViews();
				disableAllViews();
				modifyBar.setVisibility(View.GONE);
			}
		});
		
		commitBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//提交修改
				updateSpotInfo();
				disableAllViews();
				modifyBar.setVisibility(View.GONE);
			}
		});
		
		deleteBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//删除记录
				new AlertDialog.Builder(SpotDetailActivity.this)
				.setTitle("警告")
				.setMessage("请问您是否确认删除记录？")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 确定动作
						Log.d("wxq", "delete:"+spotId);
						dbTool.deleteSpot(spotId);
						finish();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 取消
						
					}
				}).show();
			}
		}); 
		
		gpsBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (! dbTool.isOutdoor()) {
					Toast.makeText(SpotDetailActivity.this, "非户外地图，无法使用本功能！", Toast.LENGTH_LONG).show();
					return;
				}
				// 跳转到GPS页面
				Intent i = new Intent();
				i.putExtra("spotId", spotId);
				i.putExtra("MallDBpath", MallDBpath);
				i.setClass(SpotDetailActivity.this, SpotGPSActivity.class);
				startActivity(i);
			}
		});
		
		unitTypeBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 可修改id
				String [] array = new String [unitTypeList.size()];
				array = DrawDBTool.getUnitTypeNames(unitTypeList).toArray(array);
				new AlertDialog.Builder(SpotDetailActivity.this)
				.setTitle("请选择")
				.setSingleChoiceItems(array, -1, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 获得选择
						unitIndex = which;
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 确定动作
						//Log.d("wxq", "选择了："+unitIndex);
						String content = "";
						if (unitIndex >= 0 && unitIndex < unitTypeList.size()) {
							content = unitTypeList.get(unitIndex).get("POITypeName");
						}
						dbTool.changeUnitType(spotId, unitTypeList.get(unitIndex).get("ID"));
						unitTypeBtn.setText(content);
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 取消	
					}
				}).show();
			}
		});
		
		fillAllViews();
		disableAllViews();
		
		moreInfoBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 调用新的页面更改。
				Intent newIntent = new Intent();
				newIntent.setClass(SpotDetailActivity.this, MoreInfoActivity.class);
				newIntent.putExtra("unitTypeId", unitTypeId);
				newIntent.putExtra("spotId", spotId);
				newIntent.putExtra("xmlContent", xmlContent);
				SpotDetailActivity.this.startActivity(newIntent);
			}
		});
	}
    protected void onResume() {
    	super.onResume();
    	//Log.d("Infor", "我靠！！！我resume了...");
    	//fillAllViews();//无需刷新页面了，请不要再调用这个函数。
    	disableAllViews();  
    }
	
	public void getAllViews() {
		//获取所有的控件
		getPicBtn = (Button) findViewById(R.id.getPicBtn);
		modifyBtn = (Button) findViewById(R.id.modifyBtn);
		modifyBar = findViewById(R.id.modifyBar);
		cancelBtn = (Button) findViewById(R.id.cancelBtn);
		commitBtn = (Button) findViewById(R.id.commitBtn);
		deleteBtn = (Button) findViewById(R.id.delBtn);
		//内容视图
		spotIdET = (EditText) findViewById(R.id.spotIdET);
		DisplayET = (EditText) findViewById(R.id.DisplayET);
		CompanyNameET = (EditText) findViewById(R.id.CompanyNameET);
		MapNameET = (EditText) findViewById(R.id.MapNameET);
		TypeET = (EditText) findViewById(R.id.TypeET);
		AddressET = (EditText) findViewById(R.id.AddressET);
		PhoneET = (EditText) findViewById(R.id.PhoneET);
		EmailET = (EditText) findViewById(R.id.EmailET);
		OpeningTimeET = (EditText) findViewById(R.id.OpeningTimeET);
		Coord_XET = (EditText) findViewById(R.id.Coord_XET);
		Coord_YET = (EditText) findViewById(R.id.Coord_YET);
		image = (EditText) findViewById(R.id.imageET);
		moreInfoLayout = (LinearLayout) findViewById(R.id.moreInfoLayout);
		moreInfoLayout.setVisibility(View.VISIBLE);
		moreInfoBtn = (Button) findViewById(R.id.moreInfoBtn);
		gpsBtn = (Button) findViewById(R.id.gpsBtn);
		unitTypeBtn = (Button) findViewById(R.id.unitTypeBtn);
	}

	public void fillAllViews() {
		//Log.d("wxq", "SpotDetailActivity:spotId="+spotId);
		Map<String, String> spotInfo = dbTool.getSpotInfo(spotId);
		spotIdET.setText(spotInfo.get("SpotID"));
		DisplayET.setText(spotInfo.get("Display"));
		CompanyNameET.setText(spotInfo.get("CompanyName"));
		MapNameET.setText(spotInfo.get("MapName"));
		TypeET.setText(spotInfo.get("Type"));
		AddressET.setText(spotInfo.get("Address"));
		PhoneET.setText(spotInfo.get("Phone"));
		EmailET.setText(spotInfo.get("Email"));
		OpeningTimeET.setText(spotInfo.get("OpeningTime"));
		Coord_XET.setText(spotInfo.get("Coord_X"));
		Coord_YET.setText(spotInfo.get("Coord_Y"));
		image.setText(spotInfo.get("image"));//lxr.....
		//获得moreInfo类型信息
		unitTypeId = spotInfo.get("UnitTypeID");
		removed = spotInfo.get("Removed");
		
		String unitTypeName = unitTypeMap.get(unitTypeId);
		if (unitTypeName != null && ! "".equals(unitTypeMap)) {
			unitTypeBtn.setText(unitTypeName);
		}
		else {
			unitTypeBtn.setText("查无此类别");
			return;
		}
		
		//Log.d("wxq","before get xmlContent ,unitTypeId="+unitTypeId+"\n"+"spotInfo="+spotInfo);
		//添加moreInfo内容
		List<Map<String, String>> list = dbTool.getPOITypeFields(unitTypeId);
		xmlContent = spotInfo.get("MoreInfo");
		if (xmlContent == null) {
			return;
		}
		Log.d("wxq",xmlContent);
		LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		Map<String, String> moreInfoMap = QUtility.getMoreInfoMap(xmlContent);
		Set<String> keys =  moreInfoMap.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			String value = moreInfoMap.get(key);
			View moreItem = inflater.inflate(R.layout.moreinfo_item, null);
			TextView nameTV = (TextView) moreItem.findViewById(R.id.name);
			TextView contentTV = (TextView) moreItem.findViewById(R.id.content);
			nameTV.setText(getChineseName(key, list)+":");
			contentTV.setText(value);
			moreInfoLayout.addView(moreItem);
		}
		
	}
	
	public String getChineseName(String field,List<Map<String, String>> list) {
		String name = field;
		for (int i = 0; i < list.size(); i++) {
			Map<String, String>	map = list.get(i);
			if (map.get("Field").equals(field)) {
				name = map.get("Name");
			}
		}
		return name;
	}
	
	public void enableAllViews() {
		DisplayET.setEnabled(true);
		CompanyNameET.setEnabled(true);
		//MapNameET.setEnabled(true);
		TypeET.setEnabled(true);
		AddressET.setEnabled(true);
		PhoneET.setEnabled(true);
		EmailET.setEnabled(true);
		OpeningTimeET.setEnabled(true);
		//Coord_XET.setEnabled(true);
		//Coord_YET.setEnabled(true);
		//image.setEnabled(true);//lxr.....
	}
	
	public void disableAllViews() {
		spotIdET.setEnabled(false);
		DisplayET.setEnabled(false);
		CompanyNameET.setEnabled(false);
		MapNameET.setEnabled(false);
		TypeET.setEnabled(false);
		AddressET.setEnabled(false);
		PhoneET.setEnabled(false);
		EmailET.setEnabled(false);
		OpeningTimeET.setEnabled(false);
		Coord_XET.setEnabled(false);
		Coord_YET.setEnabled(false);
		image.setEnabled(false);//lxr.....
	}
	
	public void updateSpotInfo() {
		if (TextUtils.isEmpty(spotId)) {
			Log.d("wxq", "无spotId,放弃更新");
			return;
		}
		ContentValues contentValues = new ContentValues();
		contentValues.put("Display", DisplayET.getText().toString());
		contentValues.put("CompanyName", CompanyNameET.getText().toString());
		contentValues.put("MapName", MapNameET.getText().toString());
		contentValues.put("Type", TypeET.getText().toString());
		contentValues.put("Address", AddressET.getText().toString());
		contentValues.put("Phone", PhoneET.getText().toString());
		contentValues.put("Email", EmailET.getText().toString());
		contentValues.put("OpeningTime", OpeningTimeET.getText().toString());
		contentValues.put("Coord_X", Coord_XET.getText().toString());
		contentValues.put("Coord_Y", Coord_YET.getText().toString());
		contentValues.put("image", image.getText().toString());//lxr.....
		if (! removed.equals("3")) {//非新增情况下修改此字段
			contentValues.put("Removed", "1");//修改
		}
		
		int resultCode = dbTool.updateSpotInfo(spotId, contentValues);
		if (resultCode>0) {
			Log.d("wxq", DisplayET.getText().toString() + "更新成功！");
		}
	}
	
	@Override
	protected void onDestroy() {
		dbTool.close();
		super.onDestroy();
	}	
}
